#include"HttpServer.hpp"
#include"Usage.hpp"
#include"Util.hpp"
#include <fstream>

//一般http要有自己的根目录
#define ROOT "./wwwroot"
/* void HandlerHttpServer(int sock)
{
    char buff[10240];
    ssize_t s=recv(sock,buff,sizeof(buff)-1,0);
    if(s>0)
    {
        buff[s]=0;
        std::cout<<buff<<std::endl;
    }
    /* std::vector<std::string> vblock;
    Util::cutString(buff,"\n",&vblock);
    std::vector<std::string> vline;
    Util::cutString(vblock[0]," ",&vline);
    std::string file=vline[1];
    if(file=="/")
    file="/index.html";
    std::string target=ROOT;
    target+=file;
    std::cout<<target<<std::endl;
    std::string content;
    std::ifstream in(target);
    if(in.is_open())
    {
        std::string line;
        while(std::getline(in, line))
        {
            content += line;
        }
        in.close();
    }

    std::string HttpResponse;
    if(content.empty()) HttpResponse = "HTTP/1.1 404 NotFound\r\n";
    else HttpResponse = "HTTP/1.1 200 OK\r\n";
    HttpResponse += "\r\n";
    HttpResponse += content;
    send(sock,HttpResponse.c_str(),sizeof(HttpResponse),0); 
    }
    */

void HandlerHttpRequest(int sockfd)
{
    // 1. 读取请求 for test
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
         std::cout << buffer << "--------------------\n" << std::endl;
    }
}
int main(int argc,const char*argv[])
{
   
    if(argc!=2)
    {
      Usage(argv[0]);
      exit(1);
    }
    uint16_t server_port=atoi(argv[1]);
    HttpServer* httpserver=new HttpServer(server_port,HandlerHttpRequest);
    httpserver->start();
   
    return 0; 
}